﻿Public Class frmProspects
    Private szSQL As String
    Dim szCritere As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0


    Private Sub frmProspects_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' afiche les DERNIERS PROSPECTS modifiés par l'agence
        szCritere = " where agence = " & gUserAgence & " and cloture_le IS NULL ORDER BY nomprenom"
        InitListeProspects(0)
    End Sub

    Private Sub InitListeProspects(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_PROSPECTS.Items.Clear()
        LV_PROSPECTS.ListViewItemSorter = Nothing

        ' Lire les 100 derniers ELEVES de l'agence
        szSQL = "SELECT cd_prospect, cree_le, civilite, nomprenom, a_relancer_le, date_relance1, date_relance2, date_relance3, statut,prochain_rdv_le" & _
                " FROM prospects " & szCritere
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            ' ignorer les activités
            LV_PROSPECTS.Items.Add(rs.Fields("cd_prospect").Value)
            LV_PROSPECTS.Items(nb).SubItems.Add(Format(rs.Fields("cree_le").Value, "dd/MM/yyyy"))
            LV_PROSPECTS.Items(nb).SubItems.Add(rs.Fields("civilite").Value)
            LV_PROSPECTS.Items(nb).SubItems.Add(nv(rs.Fields("nomprenom").Value))
            LV_PROSPECTS.Items(nb).SubItems.Add(nv(rs.Fields("a_relancer_le").Value))
            LV_PROSPECTS.Items(nb).SubItems.Add(nv(rs.Fields("date_relance1").Value))
            LV_PROSPECTS.Items(nb).SubItems.Add(nv(rs.Fields("date_relance2").Value))
            LV_PROSPECTS.Items(nb).SubItems.Add(nv(rs.Fields("date_relance3").Value))
            LV_PROSPECTS.Items(nb).SubItems.Add(IfDatetime(rs.Fields("prochain_rdv_le").Value))
            If rs.Fields("statut").Value = "Gagné" Then
                LV_PROSPECTS.Items(nb).ForeColor = Color.Green
            ElseIf rs.Fields("statut").Value = "Perdu" Then
                LV_PROSPECTS.Items(nb).ForeColor = Color.Red
            End If
            nb = nb + 1
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " lignes"
        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            SortListView(LV_PROSPECTS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_PROSPECTS)

    End Sub

    Private Sub LV_PROSPECTS_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_PROSPECTS.ColumnClick
        SortListView(LV_PROSPECTS, m_SortingColumn, e.Column)
        '        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_PROSPECTS_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_PROSPECTS.DoubleClick
        If LV_PROSPECTS.SelectedItems.Count > 0 Then
            ' afficher les détails 
            FrmProspectsMAJ.pCD_CLI = LV_PROSPECTS.SelectedItems(0).Text
            FrmProspectsMAJ.ShowDialog()
            ' réaffiche la viewlist
            InitListeProspects(LV_PROSPECTS.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_PROSPECTS_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_PROSPECTS.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter         ' Enter = DoubleClick
                LV_PROSPECTS_DoubleClick(sender, e)

            Case Keys.Insert        ' Insert = ' Ajouter un élève
                BO_ADD_LIGNE_Click(sender, e)
        End Select

    End Sub

    Private Sub TE_NOM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown
        If e.KeyCode = Keys.Enter Then
            ' nouvelle recherche
            NewSearch()
        End If

    End Sub

    Private Sub ChkCLOTURE_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkCLOTURE.CheckedChanged
        ' nouvelle recherche
        NewSearch()
    End Sub

    Private Sub NewSearch()
        ' nom saisi ?
        szCritere = " where nomprenom like '" & nq(TE_NOM.Text) & "%' "
        'If TE_NOM.TextLength Then
        '    szCritere &= " and nomprenom like '" & nq(TE_NOM.Text) & "%' "
        'End If
        If ChkCLOTURE.Checked = False Then
            szCritere &= " and cloture_le IS NULL "
        End If
        szCritere &= " ORDER BY nomprenom"
        ' affiche la listview
        InitListeProspects(0)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        FrmProspectsMAJ.pCD_CLI = 0
        FrmProspectsMAJ.ShowDialog()
        ' afiche les 50 derniers noms crées par l'agence
        szCritere = "where agence = " & gUserAgence & " ORDER BY modif_le DESC"
        InitListeProspects(0)

    End Sub

    Private Sub BO_IMPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_IMPRIMER.Click

        ' Editions des états de la rubrique
        FrmEditions.pRubrique = "PROSPECTS"
        FrmEditions.pParametres = ""
        FrmEditions.ShowDialog()

    End Sub

End Class